package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.lucher.mes.dao.WorkorderproblemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.dao.ProblemMapper;
import com.lucher.mes.po.Problem;

@Controller
@RequestMapping("/problem")  
public class ProblemController extends BaseController{
	@Autowired
	private ProblemMapper problemDao;

	@Autowired
	private WorkorderproblemMapper workorderproblemDao;
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Problem form) {
		return "problem/findProblemList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Problem form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = problemDao.findByCount(form);
		List<Problem> list = problemDao.findByListForPage(form);
		
		for (Problem problem : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", problem.getId());
			objectMap.put("problemcode", problem.getProblemcode());
			objectMap.put("problemname", problem.getProblemname());
			objectMap.put("remark", problem.getRemark());
			
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Problem form) {
		return "problem/addProblem";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	public Map<String,Object>  save(Problem form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getProblemcode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入问题编码");
			return responseJson;
		} else {
			Problem oldProblem = problemDao.findByProblemcode(form.getProblemcode());
			if (oldProblem != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此问题编码已经存在");
				return responseJson;
			}
		}
		
		//保存问题信息
		Integer problemId = problemDao.insertSelective(form);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Problem form) {
		Problem problem = problemDao.selectByPrimaryKey(form.getId());
		form.setProblem(problem);

		return "problem/updateProblem";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	public Map<String,Object> update(Problem form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getProblemcode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，问题编码不能为空");
			return responseJson;
		}

			Problem oldProblem = problemDao.findByProblemcode(form.getProblemcode());
			if (oldProblem != null && !oldProblem.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，问题编码已经存在");
				return responseJson;
			}

		//根据 原始问题代码 修改产品问题信息
		String oldCode = oldProblem.getProblemcode();
		 workorderproblemDao.updateByPrimaryCode(oldCode,form);

		// 修改信息
		 problemDao.updateByPrimaryKeySelective(form);



		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Problem form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		//删除问题
		problemDao.deleteByPrimaryKey(form.getId());
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Problem> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Problem problem : formlist) {
			//删除问题
			problemDao.deleteByPrimaryKey(problem.getId());
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
     * 获取问题的下拉列表框Json值
     */
	@ResponseBody //此标签表示返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	@RequestMapping(value="/getProblemComboBoxJson")
	public List<Map<String,Object>> getProblemComboBoxJson(Problem form) {  
	    List<Problem> problemList = problemDao.findByList(form);
	    List<Map<String, Object>> problemComboBoxJson = new ArrayList<Map<String, Object>>();
	    //添加请选择项-默认为空，查询所有
	    HashMap<String,Object> selectMap = new HashMap<String, Object>();
	    selectMap.put("id", "");
	    selectMap.put("text", "请选择");
	    problemComboBoxJson.add(selectMap);
	    
	    //封装区域属性结构信息
	    for(Problem problem:problemList){  
    	    HashMap<String,Object> problemMap = new HashMap<String, Object>();
    	    problemMap.put("id", problem.getProblemcode());
    	    problemMap.put("text", problem.getProblemname()+"("+problem.getProblemcode()+")");
    	    
    	    problemComboBoxJson.add(problemMap);  
	    }  
	   return problemComboBoxJson;
	}  
	
}
